/* 
 * Copyright (C) 2012 UTSChedule
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package utsc.apps.utschedule.classes;

import java.util.Calendar;

import android.content.Intent;
import android.text.TextUtils;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

/**
 * This is the data model used to represent an Assignment object.
 * @author 
 *
 */
@DatabaseTable
public class Assignment extends GenericTask{
	
	public final static int PRIORITY_LOW = -5;
	public final static int PRIORITY_NORMAL = 0;
	public final static int PRIORITY_INTERMEDIATE = 5;
	public final static int PRIORITY_HIGH = 10;
	public final static int PRIORITY_IMMEDIATE = 15;
	public final static int[] PRIORITY_LIST={PRIORITY_LOW,PRIORITY_NORMAL,PRIORITY_INTERMEDIATE,PRIORITY_HIGH,PRIORITY_IMMEDIATE};

	public final static int MIN=20;
	public final static int HOUR=25;
	public final static int DAY=30;
	public final static int WEEK=35;
	
	//the above vars are not in used for now
	
	/**This a constant to hold all the units we use for reminder settings.
	 * Each unit is mapped to the calendar object constant.
	 */
	public final static int[] REMINDER_LIST={Calendar.MINUTE,Calendar.HOUR_OF_DAY,Calendar.DAY_OF_YEAR,Calendar.WEEK_OF_YEAR};
	/**
	 * This constant is used for display purs
	 */
	public final static String[] REMINDER_UNIT={"minute(s)","hour(s)","day(s)","week(s)"};
	
	//useful constants for db operation
	public static final String ASSUID="_AssUID";
	public static final String TITLE="_Title";
	public static final String DUEDATE="_DueDate";
	public static final String DUETIME="_DueTime";
	
	public static final String DESCRIPTION="_description";
	public static final String COURSE="_course_id";
	public static final String REMINDER="_reminder";
	public static final String REMINDERUNIT="_reminderUnit";
	public static final String EXPIRED="_expired";
	
	@DatabaseField(generatedId = true)
	private int _AssUID;
	@DatabaseField
	private String _Title;
	@DatabaseField
	private String _DueDate;
	@DatabaseField
	private String _DueTime;
	@DatabaseField
	private int _priority = PRIORITY_NORMAL;
	@DatabaseField
	private String _description;
	@DatabaseField(foreign = true, canBeNull = false)
	private Course _course;
	@DatabaseField
	private int _reminder=1;
	@DatabaseField
	private int _reminderUnit=1;
	
	private boolean _expired=false;

	public Assignment() {
	}
	
	/**
	 * A useful constructor to take in an application intent and setup the object from it.
	 * @param intent
	 */
	public Assignment(Intent intent){
		_AssUID=intent.getIntExtra(ASSUID, -1);
		_Title=intent.getStringExtra(TITLE);
		_DueDate=intent.getStringExtra(DUEDATE);
		_DueTime=intent.getStringExtra(DUETIME);
		set_weight(intent.getDoubleExtra(WEIGHT, 0));
		set_mark(intent.getDoubleExtra(MARK, 0));
		set_total(intent.getDoubleExtra(TOTAL, 0));
		_description=intent.getStringExtra(DESCRIPTION);
		_course=new Course();
		_course.set_courseID(intent.getIntExtra(COURSE, -1));
		_reminder=intent.getIntExtra(REMINDER, 0);
		_reminderUnit=intent.getIntExtra(REMINDERUNIT, 1);
		_expired=intent.getBooleanExtra(EXPIRED, false);
	}
	
	/**
	 * A helper method to convert the current object into an application intent for data transfer among activities.
	 * @return the intent representation of the object
	 */
	public Intent getObjectIntent(){
		Intent intent=new Intent();
		intent.putExtra(Assignment.TITLE, _Title);
		intent.putExtra(Assignment.COURSE, _course.get_courseID());
		intent.putExtra(Assignment.ASSUID, _AssUID);
		intent.putExtra(Assignment.DESCRIPTION, _description);
		intent.putExtra(Assignment.DUEDATE, _DueDate);
		intent.putExtra(Assignment.DUETIME, _DueTime);
		intent.putExtra(Assignment.REMINDER, _reminder);
		intent.putExtra(Assignment.REMINDERUNIT, _reminderUnit);
		intent.putExtra(Assignment.WEIGHT, get_weight());
		intent.putExtra(Assignment.MARK, get_mark());
		intent.putExtra(Assignment.TOTAL, get_total());
		intent.putExtra(Assignment.EXPIRED, _expired);
		return intent;
	}
	
	@Override
	public String toString() {
		if (TextUtils.isEmpty(_Title)) {
			return "Assignment "+_AssUID;
		}
		return _Title;
	}

	public String get_DueTime() {
		return _DueTime;
	}

	public void set_DueTime(String _DueTime) {
		this._DueTime = _DueTime;
	}

	public String get_DueDate() {
		return _DueDate;
	}

	public void set_DueDate(String _DueDate) {
		this._DueDate = _DueDate;
	}

	public String get_Title() {
		return _Title;
	}

	public void set_Title(String _Title) {
		this._Title = _Title;
	}

	public int get_AssUID() {
		return _AssUID;
	}

	public void set_AssUID(int _AssUID) {
		this._AssUID = _AssUID;
	}

	public Course get_course() {
		return _course;
	}

	public void set_course(Course _course) {
		this._course = _course;
	}

	public int get_priority() {
		return _priority;
	}

	public void set_priority(int _priority) {
		this._priority = _priority;
	}

	public String get_description() {
		return _description;
	}

	public void set_description(String _description) {
		this._description = _description;
	}

	public int get_reminder() {
		return _reminder;
	}

	public void set_reminder(int _reminder) {
		this._reminder = _reminder;
	}

	public int get_reminderUnit() {
		return _reminderUnit;
	}

	public void set_reminderUnit(int _reminderUnit) {
		this._reminderUnit = _reminderUnit;
	}
	
	public void set_expired(boolean _expired) {
		this._expired = _expired;
	}
	
	public boolean is_expired() {
		return _expired;
	}

	/**
	 * Copy the current {@link Assignment} object.
	 */
	@Override
	public Assignment copyTask() {
		Assignment newCopy=new Assignment(getObjectIntent());
		return newCopy;
	}
	
}
